<script setup lang="ts">
import { getNetworkLocalIcon } from '@/common/util/network';
import { Wallet } from '@/types/wallet';
import { Network } from '@/types/settings';

const chainStore = useChainStore();
const walletStore = useWalletStore();
const activeChainId = ref(chainStore.currentChainId);
const selectedNetwork = ref(chainStore.currentNetwork);
const briefAccount = tool.briefAccount;
const searchName = ref('');
const netWorkIcon = ref('')
const accounts = computed(() => {
  return walletStore.wallets.filter((x) => x.chainId == activeChainId.value && (searchName.value == '' || x.name.includes(searchName.value)));
});
const emit = defineEmits(['close', 'importKey']);
// 路由动画
const decline = ref(false);
const showNetwork = ref(false)
const showWallet = ref(false)
const route = useRoute();
//展示点击账号弹出框
const changeShowWallet = () =>{
  if (showWallet.value === true){
    showWallet.value = false
  }else{
    showWallet.value = true
    showNetwork.value = false
  }
}

onMounted(() =>{
  netWorkIcon.value = getNetworkIcon(chainStore.networks[0])
})

//展示点击网络弹出框
const changeShowNetwork = () =>{
  if (showNetwork.value === true){
    showNetwork.value = false
  }else{
    showNetwork.value = true
    showWallet.value = false
  }
}

// 获取网络图标
const getNetworkIcon = (item: Network) => {
  return getNetworkLocalIcon(item.chain, item.chainId == activeChainId.value);
};

// 选择网络
const handleSelectNetwork = (item: Network) => {
  activeChainId.value = item.chainId;
  selectedNetwork.value = item;
  netWorkIcon.value = getNetworkIcon(item)
  let index = chainStore.networks.findIndex((x) => x.chainId == item.chainId && x.name == item.name);
  chainStore.setSelectedIndex(index);
  showNetwork.value = false
  emit('close');
};

// 导入密钥
const handleImportKey = (chainId: string) => {
  // alert("import .....");
  emit('importKey', chainId);
};

//打开注册页面
const openView = ()=>{
  chrome.tabs.create({
    url: 'src/entries/windows/index.html'
  });
}

// 选择账号
const handleSelectAccount = (account: Wallet) => {
  let index = walletStore.wallets.findIndex((x) => x.chainId == account.chainId && x.name == account.name);
  walletStore.setSelectedIndex(index);
  // useChainStore().setCurrentNetwork(selectedNetwork.value);
  emit('close');
};


watch(
    () => route.meta.index,
    (toIndex, fromIndex) => {
        if (toIndex > 1 && fromIndex == 1) decline.value = true;
        else if (toIndex == 1 && fromIndex > 1) decline.value = false;
    }
);
</script>

<template>
    <header class="app-header">
        <div :class="{ '_effect--50': decline }" class="_effect">
          <div class="cover-top" style="width: 50%">
            <img class="logo-img" src="@/assets/images/ngklogo.png" style="width: 66px;height: 40px;"/>
            <div @click="changeShowWallet" class="left-box">
                    <div class="account">
                        <span>
                            {{
                                walletStore.currentWallet?.name
                                    ? briefAccount(walletStore.currentWallet.name)
                                    : $t('public.noAccount')
                            }}
                        </span>
                        <span class="triangle-bottom">
                          <svg
                              xmlns="http://www.w3.org/2000/svg"
                              width="24"
                              height="24"
                              xmlns:xlink="http://www.w3.org/1999/xlink"
                              viewBox="0 0 24 24"
                              class="user_icon"
                          >
                          <path d="M7 10l5 5l5-5z" fill="currentColor">
                          </path>
                          </svg>
                        </span>
                    </div>
                </div>
            <div v-show="showWallet" class="showWalletBox">
              <n-scrollbar class="full">
                      <div class="accounts w-full">
<!--                        显示账号每一项-->
                        <div v-if="accounts.length != 0" :key="accIndex" @click="handleSelectAccount(account)" class="account-cell" v-for="(account, accIndex) in accounts">
                          <div class="account-left">
                            <div>{{account.name}}</div>
                          </div>
                          <img
                              class="close"
                              src="@/assets/images/duigou_0500ff.png"
                              v-show="walletStore.selectedIndex == accIndex"
                          />
                        </div>
                        <div v-else style="text-align: center;font-size: 16px">
                          <span>{{ $t('public.noAccount') }}</span>
                        </div>
                        <div class="accountsBottom">
                          <div @click="openView">
                            <img
                                class="add-icon"
                                src="@/assets/images/account_add.png"
                            />
                            <span>{{$t('public.AddWallet')}}</span>
                          </div>
                          <div @click="handleImportKey(chainStore.currentNetwork.chainId)">
                            <img
                                class="add-icon"
                                src="@/assets/images/importWallet.png"
                            />
                            <span>{{$t('public.importWallet')}}</span>
                          </div>
                          <div @click="$router.push({ name: 'wallet-manage' })">
                            <img
                                class="add-icon"
                                src="@/assets/images/wallet.png"
                            />
                            <span>{{$t('public.ManageWallets')}}</span>
                          </div>
                        </div>
                      </div>
                    </n-scrollbar>
            </div>
          </div>
          <div class="right-flexbox">
            <div class="right_box_item" @click="changeShowNetwork">
              <n-popover trigger="hover" :show-arrow="false">
                <template #trigger>
                  <n-button class="btn">
                    <img :src="netWorkIcon" alt="" class="right_box_img">
                  </n-button>
                </template>
                <span class="popover_span">{{$t('public.SwitchingNet')}}</span>
              </n-popover>
            </div>
            <div class="right_box_item" @click="$router.push({ name: 'network-select', query: { type: 'node' } })">
              <n-popover trigger="hover" :show-arrow="false">
                <template #trigger>
                  <n-button class="btn">
                    <img src="@/assets/images/nav2.png" alt="" class="right_box_img">
                  </n-button>
                </template>
                <span class="popover_span">{{$t('public.Address')}}</span>
              </n-popover>
            </div>
            <div class="right_box_item" @click="$router.push({name :'token-selector'})">
              <n-popover trigger="hover" :show-arrow="false">
                <template #trigger >
                  <n-button class="btn">
                    <img src="@/assets/images/nav3.png" alt="" class="right_box_img">
                  </n-button>
                </template>
                <span class="popover_span">{{$t('wallet.search')}}</span>
              </n-popover>
            </div>
            <div class="showNetwork" v-show="showNetwork">
              <n-scrollbar class="netWorkScroll" trigger="hover">
                <div class="netWorkItem"  v-for="(item, chaindex) in chainStore.networks" :key="item.chainId" @click="handleSelectNetwork(item)">
                  <div class="netWorkItemLeft">
                    <img :src="getNetworkIcon(item)"/>
                    <span>{{item.name}}</span>
                  </div>
                  <img
                      class="close"
                      src="@/assets/images/duigou_0500ff.png"
                      v-show="chainStore.selectedNetworkIndex == chaindex"
                  />
                </div>
              </n-scrollbar>
              <div class="netWorkBottom" @click="$router.push({ name: 'network-manage' })">
                <img
                    class="add-icon"
                    src="@/assets/images/guanli.png"
                />
                <span>{{$t('public.ManageNet')}}</span>
              </div>
            </div>
          </div>
        </div>
    </header>
</template>

<style scoped>
.app-header {
    position: relative;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 3;
    font-size: 19px;
    color: #ffffff;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
._effect{
    display: flex;
    justify-content: space-between;
    align-items: center;
    justify-items: center;
    width: 100%;
}
.logo-img {
    width: 45px;
    height: 32px;
}

.left-box {
  font-size: 12px;
  color: #ffffff;
  font-weight: 400;
  height: 32px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.account {
  cursor: pointer;
  padding: 0 10px;
  border-radius: 16px;
  height: 32px;
  line-height: 32px;
  font-size: 14px;
  color: #1E2329;
  font-weight: 500;
  display: flex;
  align-items: center;
}

.account>span:first-child{
  display: inline-block;
}

.showWalletBox{
  position: absolute;
  top: 60px;
  left: 0;
  width: 380px;
  z-index: 97;
  background-color: #ecf5ff;
  text-align: center;
  box-shadow: 0 1px 1px #409eff;
}

.triangle-bottom {
  display: inline-block;
  margin-left: 5px;
}
.triangle-bottom>svg{
  width: 20px;
  height: 20px;
}
.user_icon{
  color: rgb(146,154,165);
}
.right-flexbox{
  display: flex;
  width: 40%;
  justify-content: space-around;
  align-items: center;
  position: relative;
}
.right_box_item{
  width: 40px;
  height: 32px;
}
.right_box_img{
  transform: scale(-2) rotate(180deg);
}
.btn{
  background-color: rgb(0,0,0,0);
  width: 40px;
  height: 32px;
}

.showNetwork{
  position: absolute;
  top: 50px;
  right: 0;
  width: 380px;
  z-index: 97;
  background-color: #ecf5ff;
  text-align: center;
  color: #1E2329;
  box-shadow: 0 1px 1px #409eff;
  padding: 10px 15px;
}

.netWorkScroll{
  width: 100%;
}

.netWorkItem{
  width: 100%;
  text-align: left;
  height: 24px;
  color: #000;
  padding: 15px;
  margin-bottom: 10px;
  font-size: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.netWorkItemLeft>img{
  width: 25px;
  height: 25px;
  margin-left: 15px;
  display: inline-block;
}

.netWorkItemLeft>span{
  margin-left: 20px;
  color: #1E2329;
  width: 50%;
  height: 100%;
  font-size: 16px;
}
.netWorkBottom{
  display: flex;
  justify-content: left;
  align-items: center;
  justify-items: center;
  font-size: 14px;
  padding-left: 35px;
  height: 40px;
  border-top: 1px solid rgb(234,236,239);
}
.netWorkBottom>span{
  margin: 0 10px;
}
.popover_span{
  padding: 8px 20px;
  background-color: #ecf5ff;
  color: #000;
}


</style>
<style lang="scss" scoped>
.account-selector {
  position: absolute;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 97;
}

.bg-img {
  width: 7.5px;
  height: auto;
  margin-right: 15px;
}
.lock-icon {
  height: 21px;
  width: auto;
  cursor: pointer;
}
.close {
  cursor: pointer;
  width: 14px;
  height: auto;
}
.add-icon {
  width: auto;
  height: 16px;
  cursor: pointer;
}
.import {
  cursor: pointer;
}

.list-container {
  padding: 0;
  overflow: hidden;
  height: 400px;

  :deep(.n-tabs-tab-pad) {
    display: none !important;
  }

  .icon-img {
    width: 30px;
    height: 30px;
    border-radius: 15px;
  }

  .import-key-btn {
    margin-top: 15px;
    background: $color-primary;
    border-radius: 6px;
    width: 100%;
    height: 40px;
    font-size: 15px;
    font-weight: 400;
    color: #ffffff;
    letter-spacing: 0;
  }
}

.accounts {
  padding: 10px 15px;
  overflow: visible;
  display: flex;
  flex-direction: column;
  color: #000000;

  .title-cell {
    font-size: 16px;
    color: #222;
    height: 40px;
    border-bottom: 1px solid $color-separate;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .actions {
    display: flex;
    flex-direction: row;
    align-items: center;
    height: 24px;
    line-height: 1em;
    .search-acts {
      margin-right: 8px;
      display: flex;
      flex-direction: row;
      align-items: center;
      border-bottom: #ddd 1px solid;
      height: 24px;
      i {
        font-size: 12px;
        margin-right: 3px;
      }
      input {
        border: none;
        width: 70px;
        color: #666;
        font-size: 12px;
        &::placeholder {
          color: #999;
        }
      }
    }
  }

  .account-cell {
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 24px;
    color: #000;
    padding: 15px;
    margin-bottom: 10px;
    font-size: 16px;

    .account-left {
      display: flex;
      flex-direction: column;
      justify-content: center;

      .account-left-name {
        font-size: 14px;
        color: #ffffff;
        font-weight: 600;
      }
      .account-left-key {
        color: #ffffff;
        display: flex;
        flex-direction: row;
        font-size: 12px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }

  .accountsBottom{
    display: flex;
    justify-content: space-around;
    border-top: 1px solid rgb(234,236,239);
  }
}
.accountsBottom>div{
  display: flex;
  justify-items: center;
  align-items: center;
  font-size: 14px;
}

.accountsBottom>div>img{
  margin-right: 5px;
}
</style>
